﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace DomainModel
{
    public class Address
    {
        private List<string> _errors;

        public virtual string Line1 { get; set; }
        public virtual string Line2 { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string PostalCode { get; set; }

        public List<string> Errors
        {
            get
            {
                _errors = new List<string>();
                Validate();
                return _errors;
            }
        }

        public bool IsValid
        {
            get
            {
                return Errors.Count == 0;
            }
        }

        public Address()
        {
            _errors = new List<string>();
        }

        public Address(IDataReader reader)
            : this()
        {
            Line1 = reader["HomeAddressLine1"] is DBNull ? null : (string)reader["HomeAddressLine1"];
            Line2 = reader["HomeAddressLine2"] is DBNull ? null : (string)reader["HomeAddressLine2"];
            City = reader["HomeAddressCity"] is DBNull ? null : (string)reader["HomeAddressCity"];
            State = reader["HomeAddressState"] is DBNull ? null : (string)reader["HomeAddressState"];
            PostalCode = reader["HomeAddressPostalCode"] is DBNull ? null : (string)reader["HomeAddressPostalCode"];
        }

        private void Validate()
        {
            ValidateLine1();
            ValidateLine2();
            ValidateCity();
            ValidateState();
            ValidatePostalCode();
        }

        private void ValidateLine1()
        {
            Line1CannotBeMoreThan100Characters();
        }

        private void Line1CannotBeMoreThan100Characters()
        {
            if (!string.IsNullOrWhiteSpace(Line1) && Line1.Length > 100)
            {
                _errors.Add("Line 1 cannot be more than 100 characters.");
            }
        }

        private void ValidateLine2()
        {
            Line2CannotBeMoreThan100Characters();
        }

        private void Line2CannotBeMoreThan100Characters()
        {
            if (!string.IsNullOrWhiteSpace(Line2) && Line2.Length > 100)
            {
                _errors.Add("Line 2 cannot be more than 100 characters.");
            }
        }

        private void ValidateCity()
        {
            CityCannotBeMoreThan30Characters();
        }

        private void CityCannotBeMoreThan30Characters()
        {
            if (!string.IsNullOrWhiteSpace(City) && City.Length > 30)
            {
                _errors.Add("City cannot be more than 30 characters.");
            }
        }

        private void ValidateState()
        {
            StateCannotBeMoreThan30Characters();
        }

        private void StateCannotBeMoreThan30Characters()
        {
            if (!string.IsNullOrWhiteSpace(State) && State.Length > 30)
            {
                _errors.Add("State cannot be more than 30 characters.");
            }
        }

        private void ValidatePostalCode()
        {
            PostalCodeCannotBeMoreThan10Characters();
        }

        private void PostalCodeCannotBeMoreThan10Characters()
        {
            if (!string.IsNullOrWhiteSpace(PostalCode) && PostalCode.Length > 10)
            {
                _errors.Add("Postal code cannot be more than 10 characters.");
            }
        }
    }
}
